package querysrc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.subway.babynote.syncdata.*;


import serverdata.ServerDataRecord;
import serverdata.cache_Manager;

public class query_login extends query {
	

	public query_login(sqlConnectionHandler sqlconhandler)
	{
		super(sqlconhandler);
	}
	//return user_id
	public int login(ClientDataRecord.usersRecord ur)
	{
		Connection con = null;
		ResultSet rs = null;
		querydb qdb =null;
		int iret = -1;
		try
		{
			boolean bret = true;
			con = this.init();	
			if(bret)
			{
				qdb = new querydb(con, sqlscripts.sql_reg_user_select_uid);				
				bret = qdb.prePS();				
			}
			if(bret)
			{
				qdb.ps.setString(1, ur.email);				
				rs = qdb.execPSQuery();
				if (!rs.next()){
					//no user
					iret = errMessage.err_nouser.errcode;				
			      }
			      else {    	 
			    	  int user_id = rs.getInt(sqlscripts.sql_reg_user_select_uid_c1_uid);
			    	  String imsi = rs.getString(sqlscripts.sql_reg_user_select_uid_c2_imsi);
			    	  String imei = rs.getString(sqlscripts.sql_reg_user_select_uid_c3_imei);
			    	  String passwd = rs.getString(sqlscripts.sql_reg_user_select_uid_c4_passwd);
			    	  java.sql.Date birth = rs.getDate(sqlscripts.sql_reg_user_select_uid_c5_birth);
			    	  boolean gender = rs.getBoolean(sqlscripts.sql_reg_user_select_uid_c6_gender);
			    	  int uts = rs.getInt(sqlscripts.sql_reg_user_select_uid_c7_ts);
			    	  
			    	  if(ur.passwd.equals(passwd))
			    	  { //ok
			    		  iret = user_id;
			    		  ur.birth = birth;
			    		  ur.gender = gender;
			    		  ur.user_id = user_id;
			    		  ur.ts = uts;
			    		  ur.passwd = "";			    		  
			    		  if(ur.imsi.equals(imsi) && ur.imei.equals(imei))
		    			  {//nothing todo
			    			  ur.user_id = user_id;		
			    			  
		    			  }
			    		  else  //update imsi, imei 
			    		  {
			    			  //update cache
			    			  //ServerDataRecord.userAuthenticateRecord uar = new ServerDataRecord.userAuthenticateRecord(ur.imsi,imei);
			    			  //cache_Manager.get_userCache().putValue(user_id, uar);
			    			
			    			  if(this.update_user_mobile(con,user_id, ur.imsi, ur.imei)!=1)
			    				  iret = errMessage.err_updatedb.errcode; //update imsi, imei error
			    		  }			    
			    		  //ur.imei = imsi;
			    		  //ur.imsi = imei;
			    		  ServerDataRecord.userAuthenticateRecord uar = new ServerDataRecord.userAuthenticateRecord(ur.imsi,ur.imei);
			    		  cache_Manager.get_userCache().putValue(user_id, uar);
			    	  }
			    	  else
			    		  iret = errMessage.err_passwd.errcode; //passwd error
			        }
				
			}
			
			return iret;
			
		}
		catch(SQLException se) {
		      System.out.println("SQLException: "+se.getMessage());
		      return -1;
	    }
		catch(Exception e)
		{
			e.printStackTrace();
			return -1;
		}
		finally
		{
			this.release_resultset(rs);
			this.release_querydb(qdb);
			this.release_connection(con);
			this.finalize();
		}
	}
	
	private int update_user_mobile(Connection con, int user_id, String imsi, String imei)
	{
		querydb qdb = null;

		try
		{
			boolean bret = true;
			int iret = -1;
			qdb = new querydb(con,sqlscripts.sql_login_user_update);
			bret = qdb.prePS();
			if(bret)
			{
				qdb.ps.setString(1, imsi);
				qdb.ps.setString(2, imei);
				qdb.ps.setInt(3, user_id);
				
				iret = qdb.execPSUpdate();
			}				
			return iret;			
		}
		catch(SQLException se) {
		      System.out.println("SQLException: "+se.getMessage());
		      return -1;
	    }
		catch(Exception e)
		{
			e.printStackTrace();
			return -1;
		}
		finally
		{
			this.release_querydb(qdb);
		}
	}
}
